<template>
	<view>
		<mescroll-body ref="mescrollRef" :down="downOption" :up="upOption" @up="upCallback"><good-list :list="goods"></good-list></mescroll-body>
	</view>
</template>

<script>
import goodList from '@/pages/aduit/components/good-list.vue';

import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
var _self; //this容器
export default {
	mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
	components: {
		goodList
	},
	data() {
		return {
			code: '',
			goods: [],
			totalPage: 0, //总页数
			downOption: {
				use: false // 不使用下拉刷新
			},
			upOption: {
				noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
				empty: {
					tip: '~ 搜索无结果 ~' // 提示
				}
			}
		};
	},

	onLoad(options) {
		_self = this;
		_self.code = options.code;
	},
	methods: {
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) {
			//联网加载数据
			if (_self.isChangeTab) {
				_self.mescroll.showUpScroll();
			}
			_self
				.getListDataFromNet(page.num, page.size)
				.then(
					curPageData => {
						if (page.num == 1) _self.goods = []; //如果是第一页需手动制空列表
						_self.goods = _self.goods.concat(curPageData); //追加新数据
						_self.$nextTick(() => {
							_self.mescroll.endByPage(curPageData.length, _self.totalPage);
							_self.mescroll.hideUpScroll();
						});
					},
					error => {
						_self.mescroll.endByPage(0, 0);
						_self.mescroll.hideUpScroll();
					}
				)
				.catch(() => {
					//联网失败, 结束加载
					_self.mescroll.endErr();
				});
		},

		/* 获取查询数据 */
		getListDataFromNet(pageNum, pageSize, successCallback, errorCallback) {
			try {
				const param = {
					currentPage: pageNum,
					pageSize: pageSize,
					condition: {
						catalogId: _self.code
					}
				};
				let promise = new Promise(function(resolve, reject) {
					_self.$http.httpRequest('taobao/productRecommend/catalogProductPagination', param, false).then(res => {
						if (res._tranStatus) {
							_self.totalPage = res._data.totalPage;
							resolve(res._data.pageItems);
						} else {
							let error = {};
							reject('error');
						}
					});
				});
				return promise;
			} catch (e) {
				return Promise.reject(e);
			}
		}
	}
};
</script>
